<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	页面列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$_SESSION['mobile_web']['tpl_id']=intval($_RQ['id']);
	}
	if(!$_SESSION['mobile_web']['tpl_id']){
		exi('请先选择模板！','error');
	}
	$web_id=pdo_getcolumn('core_account_web_tpl',array('id'=>$_SESSION['mobile_web']['tpl_id']),'web_id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'tpl_id'=>$_SESSION['mobile_web']['tpl_id']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','web_id','title','createtime','html');
	$list=pdo_getslice('core_account_web_tpl',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		$default_mobile_web=uni_setting('default_mobile_web');
		foreach($list as &$item){
			$item['is_default']=($item['id']==$default_mobile_web)?1:0;
			$item['url']=custom_url('core/welcome//pageid='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('action'=>'/wxapp.app_web_tpls//web_id='.$web_id,'title'=>'返回模板'),
			array('op'=>'list','title'=>'页面列表'),
		),
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：每个微站是可以设置多个页面，微站之间可以自由链接，比如：首页，文章列表页，产品页，内容页等配套使用就形成一个丰富的微官网！','list'=>array(
			array('name'=>'title','title'=>'页面名称','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'url','title'=>'体验二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('action'=>'wxapp.list','op'=>'design','title'=>'设计','type'=>'href'),
		)),
		'search_one'=>array('select_name'=>'group_id','options'=>'groups','name'=>'title','add'=>true),
	);
	if($_SESSION['isfounder']){
		$htmls['tabs'][]=array('action'=>'cloud.mobile_blocks','op'=>'prepared','title'=>'安装页面组件');
	}
	$return['htmls']=$htmls;
	set_html('header','common/header');
	set_html('title','模板子页管理');
	exi($return);
}
if($_RQ['op']=='edit'){
	//编辑微站，添加微站在微站模版列表那边选择模版然后添加
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','status','foot'));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置页面信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'页面列表'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			array('type'=>'switch','name'=>'status','title'=>'是否启用'),
			array('type'=>'text','name'=>'foot','title'=>'底部自定义'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	$data=array_fields(array('title','status','foot'),$post);
	if($id){
		pdo_update('core_account_web_tpl',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('保存成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['web_id']=pdo_getcolumn('core_account_web_tpl',array('id'=>$_SESSION['mobile_web']['tpl_id'],'uniacid'=>$_SESSION['uniacid']),'web_id');
		$data['tpl_id']=$_SESSION['mobile_web']['tpl_id'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_web_tpl',$data);
		exi('创建成功！','','list');
	}
}
if($_RQ['op']=='link'){
	$list=pdo_getall('core_account_web_tpl',array('tpl_id'=>$_SESSION['mobile_web']['tpl_id']),array('id','title'));
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('core/welcome/main/pageid='.$item['id']);
		}
	}
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'link','title'=>'站内页面'),
			array('action'=>'tools.link','title'=>'系统其他页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'pagelist'=>false,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'url','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='getnav'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}